/*
 * @Author: TT-hzy 
 * @Date: 2024-06-29 17:07:23
 * @LastEditors: TT-hzy 
 * @LastEditTime: 2024-07-02 18:44:15
 * @Description: 
 */


import { Context } from 'egg'

export default {
  success({
    msg = "成功",
    data
  }: {
    msg?: string,
    data?: any,
  }) {
    const isObj = typeof msg === 'object';
    isObj && (data = msg);
    return {
      code: 0,
      data: data || {},
      msg: isObj ? '成功' : msg,
    }
  },


  configReturnBody({
    ctx,
    isOk = true,
    msg = "成功",
    data
  }: {
    ctx: Context,
    isOk?: boolean,
    msg?: string,
    data?: any
  }) {
  
    return ctx.body = isOk ?
      this.success({
        msg,
        data
      }) :
      this.fail({
        msg,
        data
      });
  },


  fail({
    msg = "失败",
    data
  }: {
    msg?: string,
    data?: any,
  }) {
    const isObj = typeof msg === 'object';
    isObj && (data = msg);
    return {
      code: 1,
      data: data || {},
      msg: isObj ? '失败' : msg,
    }
  }
}